package com.brand.data.util;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

public class TipsUtils {
	private static final String OPEN_ID="oM0Nf1rzGJ9IMPwr1pAVAAgdhx3A";
	
	private static final long diff = 1000 * 60 * 10;
	private static Map<String, Long> timeMap = new ConcurrentHashMap<>();
	
	public static void message(String project, String title, String content){
		message(OPEN_ID, project, title, content, null, true);
	}
	public static void message(String openid, String project, String title, String content){
		message(openid, project, title, content, null, true);
	}
	
	public static void messUrl(String project, String title, String content, String url){
		message(OPEN_ID, project, title, content, url, true);
	}
	public static void message(String openid, String project, String title, String content, String url){
		message(openid, project, title, content, url, true);
	}
	
	public static void message(String openid, String project, String title, String content, String url, boolean repeat){
		if(!repeat){
			if(timeMap.containsKey(title) && System.currentTimeMillis() - timeMap.get(title) < diff){
				return ;
			}
		}
		try {
			Connection conn = Jsoup.connect("https://faysunshine.com/server-wechat/notice/tips/" + project).ignoreContentType(true);
			conn.data("openid", openid);
			conn.data("title", title);
			conn.data("content", content);
			if(url != null) conn.data("url", url);
			Response response = conn.method(Method.POST).execute();
			timeMap.put(title, System.currentTimeMillis());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		TipsUtils.message("oM0Nf1h8Rn_jhUP7VQkZiWs9lSsA","北京现代", "服务下线", "服务下线");
	}
}
